/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 07 - 04
 * Time: 10:40
 */
class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
}
public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode cur = head.next;
        ListNode node = head;
        node.next = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = node;
            node = cur;
            cur = curNext;
        }
        head = node;
        return head;
    }
}

class Solution1 {
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            ListNode curNext = cur.next;//记录需要反转节点的下一个节点
            cur.next = head;//用头插法
            head = cur;
            cur = curNext;
        }
        return head;
    }
}